package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.entity.ZjAccount;
import com.zb.entity.ZjWorkModule;
import com.zb.entity.ZjWorkOrder;
import com.zb.entity.ZjWorkUser;
import com.zb.service.*;
import com.zb.sms.entity.OrderReceiveVo;
import com.zb.view.ViewNetworkEmpAccount;
import com.zb.view.ViewNetworkEmpDetail;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 工单工具服务类
 * @Author WEN
 * @Date 2024/5/23 0023
 **/
@Service
public class WorkUtilService {
    private static WorkUtilService workUtilService;
    @PostConstruct
    public void init() {
        workUtilService = this;
    }
    @Resource
    private ZjWorkOrderService workOrderService;
    @Resource
    private ZjAccountService accountService;
    @Resource
    private ViewNetworkEmpAccountService viewNetworkEmpAccountService;
    @Resource
    private ViewNetworkEmpDetailService viewNetworkEmpDetailService;
    @Resource
    private ZjWorkUserService workUserService;
    @Resource
    private ZjWorkModuleService workModuleService;

    /**
     * 获取工单通知信息接收人员
     * @param workId
     * @return
     */
    public static OrderReceiveVo getOrderReceive(Long workId) {
        QueryWrapper<ZjWorkOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.select("id", "code", "module_id", "emp_id", "network_id", "network_emp_id")
                        .eq("id", workId);
        ZjWorkOrder order = workUtilService.workOrderService.getOne(orderQueryWrapper);
        OrderReceiveVo receiveVo = new OrderReceiveVo();
        receiveVo.setWorkNo(order.getCode());
        receiveVo.setModule(workUtilService.workModuleService.getPartInfoById(order.getModuleId()).getName());
        // 客服
        if (order.getEmpId() > 0) {
            ZjAccount account = workUtilService.accountService.getById(order.getEmpId());
            if (null != account) receiveVo.setEmpPhone(account.getPhone());
        }
        if (order.getNetworkId() > 0) {
            ViewNetworkEmpDetail empDetail = workUtilService.viewNetworkEmpDetailService.getManagerByNetworkId(order.getNetworkId());
            if (null != empDetail) receiveVo.setNetworkManagerPhone(empDetail.getAccountPhone());
        }
        if (order.getNetworkEmpId() > 0) {
            ViewNetworkEmpAccount empAccount = workUtilService.viewNetworkEmpAccountService.getById(order.getNetworkEmpId());
            if (null != empAccount) {
                receiveVo.setEngineer(empAccount.getName());
                receiveVo.setEngineerPhone(empAccount.getPhone());
            }
        }
        QueryWrapper<ZjWorkUser> workUserQueryWrapper = new QueryWrapper<>();
        workUserQueryWrapper.select("id", "name", "phone")
                .eq("id", workId)
                .last("LIMIT 1");
        ZjWorkUser workUser = workUtilService.workUserService.getOne(workUserQueryWrapper);
        receiveVo.setUsername(workUser.getName());
        receiveVo.setUserPhone(workUser.getPhone());
        return receiveVo;
    }

    /**
     * 获取工单通知信息接收人员
     * @param workIds
     * @return
     */
    public static List<OrderReceiveVo> getOrderReceive(List<Long> workIds) {
        QueryWrapper<ZjWorkOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.select("id", "code", "module_id", "emp_id", "network_id", "network_emp_id")
                .in("id", workIds);
        List<ZjWorkOrder> orders = workUtilService.workOrderService.list(orderQueryWrapper);
        // 客服
        List<Long> empIds = orders.stream().map(ZjWorkOrder::getEmpId).distinct().toList();
        // 服务商ID
        List<Long> networkIds = orders.stream().map(ZjWorkOrder::getNetworkId).distinct().toList();
        // 服务商人员ID
        List<Long> networkEmpIds = orders.stream().map(ZjWorkOrder::getNetworkEmpId).distinct().toList();
        List<ZjAccount> accountList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(empIds)) {
            accountList.addAll(workUtilService.accountService.getByIds(empIds));
        }
        List<ViewNetworkEmpDetail> empDetailList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(networkIds)) {
            empDetailList.addAll(workUtilService.viewNetworkEmpDetailService.getManagerByNetworkIds(networkIds));
        }
        List<ViewNetworkEmpAccount> empAccountList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(networkEmpIds)) {
            empAccountList.addAll(workUtilService.viewNetworkEmpAccountService.getByIds(networkEmpIds));
        }
        QueryWrapper<ZjWorkUser> workUserQueryWrapper = new QueryWrapper<>();
        workUserQueryWrapper.select("id", "name", "phone")
                .in("id", workIds);
        List<ZjWorkUser> userList = workUtilService.workUserService.list(workUserQueryWrapper);
        List<ZjWorkModule> moduleList = workUtilService.workModuleService.getPartInfoByIds(orders.stream().map(ZjWorkOrder::getModuleId).distinct().toList());
        List<OrderReceiveVo> receiveVos = new ArrayList<>();
        OrderReceiveVo receiveVo = null;
        ZjAccount account = null;
        ViewNetworkEmpDetail networkDetail = null;
        ViewNetworkEmpAccount networkEmpAccount = null;
        ZjWorkUser workUser = null;
        for (ZjWorkOrder order : orders) {
            receiveVo = new OrderReceiveVo();
            receiveVo.setWorkId(order.getId());
            receiveVo.setWorkNo(order.getCode());
            receiveVo.setModule(moduleList.stream().filter(f -> Objects.equals(f.getId(), order.getModuleId())).findFirst().map(ZjWorkModule::getName).orElse(""));
            account = accountList.stream().filter(f -> Objects.equals(f.getId(), order.getEmpId())).findFirst().orElse(null);
            if (null != account) receiveVo.setEmpPhone(account.getPhone());
            networkDetail = empDetailList.stream().filter(f -> Objects.equals(f.getNetworkId(), order.getNetworkId())).findFirst().orElse(null);
            if (null != networkDetail) receiveVo.setNetworkManagerPhone(networkDetail.getAccountPhone());
            networkEmpAccount = empAccountList.stream().filter(f -> Objects.equals(f.getId(), order.getNetworkEmpId())).findFirst().orElse(null);
            if (null != networkEmpAccount) {
                receiveVo.setEngineer(networkEmpAccount.getName());
                receiveVo.setEngineerPhone(networkEmpAccount.getPhone());
            }
            workUser = userList.stream().filter(f -> Objects.equals(f.getId(), order.getId())).findFirst().orElse(null);
            if (null != workUser) {
                receiveVo.setUsername(workUser.getName());
                receiveVo.setUserPhone(workUser.getPhone());
            }
            receiveVos.add(receiveVo);
        }
        return receiveVos;
    }
}
